package com.kordar.api.unicomJS;

import com.alibaba.fastjson.JSONObject;
import com.kordar.api.modular.unicomJS.ZOPCheckAuthRequestImpl;
import com.kordar.api.util.AesUtil;
import com.kordar.api.modular.unicomJS.SysParams;
import com.kordar.api.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Slf4j
@SpringBootTest
public class UnicomJS {

    @Autowired
    RestTemplate restTemplate;

    @Test
    void testPost() {
        // 请求头信息
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");

        // 请求体内容
//        Request request = new ZOPChoiceNumberRequestImpl();
        ZOPCheckAuthRequestImpl request = new ZOPCheckAuthRequestImpl();
        request.setProvince("34");
        request.setCity("340");
        request.setCertName("123123");
        request.setCertNum("3434");
        JSONObject baseObj = new JSONObject();
        JSONObject requestInfo = new JSONObject();

        log.info("================request head======================");
        JSONObject headObj = request.head();
        requestInfo.put("head", headObj);
        log.info(headObj.toJSONString());

        log.info("================request body======================");
        JSONObject bodyObj = request.body();
        requestInfo.put("body", bodyObj);
        log.info(bodyObj.toJSONString());
        baseObj.put("appCode", SysParams.SYS_APP_CODE.getName());
        baseObj.put("requestInfo", AesUtil.encrypt(requestInfo.toString(), SysParams.SYS_SECRET_KEY.getName()));
        log.info("================base======================");
        log.info(baseObj.toJSONString());


        // 组装请求信息
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(baseObj, headers);

        JSONObject result = restTemplate.postForObject(request.url(), httpEntity, JSONObject.class);
        assert result != null;

        String responseInfo = result.getString("responseInfo");
        //解密验签
        String decrypt = AesUtil.decrypt(responseInfo, SysParams.SYS_SECRET_KEY.getName());
        JSONObject responseJson = JSONObject.parseObject(decrypt);
        JSONObject head = responseJson.getJSONObject("head");
        JSONObject body = responseJson.getJSONObject("body");
        System.out.println(body);
        String _timestamp = head.getString("timestamp");
        String _uuid = head.getString("uuid");
        String _sign = head.getString("sign");
        String $sign = SysParams.getSign(_timestamp, _uuid);
        if ($sign.equals(_sign)) {
            System.out.println("验签成功");
        } else {
            System.out.println("验签失败");
        }

        /*responseJson.replace("responseInfo", responseInfo, JSONObject.parseObject(decrypt));
        System.out.println("解密报文返回:" + responseJson);

        String _timestamp = head.getString("timestamp");
        String _uuid = head.getString("uuid");
        String _sign = head.getString("sign");
        String _appCode = responseJson.getString("appCode");*/



        /*if (responseJson != null) {
            String responseInfo = responseJson.getString("responseInfo");
            //解密验签
            String decrypt = AESUtil.decrypt(responseInfo, secretKey);
            responseJson.replace("responseInfo", responseInfo, JSONObject.parseObject(decrypt));
            System.out.println("解密报文返回:" + responseJson);
            JSONObject head = responseJson.getJSONObject("responseInfo").getJSONObject("head");
            String _timestamp = head.getString("timestamp");
            String _uuid = head.getString("uuid");
            String _sign = head.getString("sign");
            String _appCode = responseJson.getString("appCode");

            Map<String, String> map = new HashMap<>();
            map.put("appCode", _appCode);
            map.put("uuid", _uuid);
            map.put("timestamp", _timestamp);
            map.put("secretKey", secretKey);


            String $sign = RspUtil.sign(map);
            if ($sign.equals(_sign)) {
                System.out.println("验签成功");
            } else {
                System.out.println("验签失败");
            }

        }*/

    }

    @Test
    public void testToken() {
        System.out.println(JwtTokenUtil.generateToken("unicomjs", new Date(System.currentTimeMillis() + 86400 * 1000), null));
    }

    @Test
    public void testJson() {
        JSONObject object = JSONObject.parseObject("{\"orderNo\":\"2560112642795026\",\"token\":\"x7fHub9URZl8qRlMVBKkrh+qMKS/gOgptN28w60tKu7ZK7lldlM+bEh4Q3mpaCd4+ESPxVLRgeEemVI1PorX3fE5ltDDo1fcT58OPe0TsGNJRexQcqrVx84QODRc83ipWMPVDDQntE7nlA8lUmel4QVYW5ciTMayU8=\"}");
        System.out.println(object.getString("token"));
    }

    @Test
    public void testGood() {
        String good = "981909126138";
        String pre = "aa";
        System.out.println(pre + good.substring(2));
    }

}
